home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr25 / rxcalc.zip / SQRT.CAL < prev    next >
Text File  |  1993-03-01  |  929b  |  27 lines

  1. /* Function to calculate the square root of a number using */
  2. /* the Newton-Raphson method               (GKB/SSK 11/90) */
  3.  
  4. arg num                             /* get the number      */
  5.  
  6. if ^datatype(num,'Number')          /* valid number?       */
  7.   then return
  8.  
  9. if num < 0                          /* check for negative  */
  10.   then return
  11.   else if num = 0 then return 0     /* check for 0         */
  12.  
  13. xnew = num                          /* initialize answer   */
  14.  
  15.                                     /* calculate maximum   */
  16. eps = 0.5 * 10**(1+fuzz()-digits()) /* accuracy            */
  17.  
  18. /* Loop until a sufficiently accurate answer is obtained.  */
  19.  
  20. do until abs(xold-xnew) < (eps*xnew)
  21.   xold = xnew                       /* save the old value  */
  22.   xnew = 0.5 * (xold + num / xold)  /* calculate the new   */
  23. end
  24. xnew = xnew / 1                     /* make it pretty      */
  25.  
  26. return xnew
  27.